package ezy.lite.util;

import java.util.Random;

public class RandomUtil {

    public static final String NUMBERS = "0123456789";
    public static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
    public static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";


    public static String numbers(int length) {
        return gen(NUMBERS, length);
    }
    public static String uppercase(int length) {
        return gen(UPPERCASE, length);
    }
    public static String lowercase(int length) {
        return gen(LOWERCASE, length);
    }
    public static String letters(int length) {
        return gen(LETTERS, length);
    }
    public static String chars(int length) {
        return gen(CHARS, length);
    }

    public static String gen(String source, int length) {
        return gen(source.toCharArray(), length);
    }

    public static String gen(char[] source, int length) {
        if (source == null || source.length == 0 || length < 0) {
            return null;
        }

        StringBuilder result = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            result.append(source[random.nextInt(source.length)]);
        }
        return result.toString();
    }

    public static int next(int max) {
        return next(0, max);
    }

    public static int next(int min, int max) {
        if (min > max) {
            return 0;
        }
        if (min == max) {
            return min;
        }
        return min + new Random().nextInt(max - min);
    }

    public static boolean shuffle(Object[] array) {
        if (array == null) {
            return false;
        }

        return shuffle(array, next(array.length));
    }

    public static boolean shuffle(Object[] array, int count) {
        if (array == null || count < 0 || array.length < count) {
            return false;
        }
        int length = array.length;

        for (int i = 1; i <= count; i++) {
            int random = next(length - i);
            Object temp = array[length - i];
            array[length - i] = array[random];
            array[random] = temp;
        }
        return true;
    }

    public static int[] shuffle(int[] array) {
        if (array == null) {
            return null;
        }

        return shuffle(array, next(array.length));
    }

    public static int[] shuffle(int[] array, int count) {
        if (array == null || count < 0 || array.length < count) {
            return null;
        }
        int length = array.length;

        int[] out = new int[count];
        for (int i = 1; i <= count; i++) {
            int random = next(length - i);
            out[i - 1] = array[random];
            int temp = array[length - i];
            array[length - i] = array[random];
            array[random] = temp;
        }
        return out;
    }
}